package com.welsy.stats.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.welsy.iter.business.*;
	import com.welsy.util.Comparator;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.events.FaultEvent;
	import mx.controls.Alert;
	import mx.collections.ICollectionView;
	import mx.collections.Sort;
	import mx.collections.SortField;
	import mx.utils.ArrayUtil;
	import com.welsy.iter.vo.IterComponentsVO;
	import com.welsy.stats.model.StatsModelLocator;
	import com.welsy.util.NamespaceLocator;
	import com.welsy.iter.vo.IterVO;
	import mx.collections.ArrayCollection;
	import mx.rpc.IResponder;
	import com.welsy.stats.event.GetPublishItersEvent;
	import com.welsy.stats.business.GetPublishItersDelegate;
	
	public class GetPublishItersCommand implements ICommand, IResponder
	{
	  	public function GetPublishItersCommand()
		{	 
		}
	
		public function execute( event : CairngormEvent ): void
		{
			var eve : GetPublishItersEvent = GetPublishItersEvent(event);
			var delegate : GetPublishItersDelegate = new GetPublishItersDelegate(this);
			delegate.getPublishIters(eve.authorId);
		}
	
		public function result( event : Object ) : void
		{				
			var xres : XMLList = XMLList(ResultEvent(event).result);
			var iNS : Namespace = new Namespace(NamespaceLocator.ITER_DATATYPES);
			
			var TAG_GETPUBLISHITERSRESULT : QName = new QName(iNS, "GetPublishItersResult");
			var TAG_ITERSCOLLECTION : QName = new QName(iNS, "ItersCollection");
			var TAG_ITERTYPE : QName = new QName(iNS, "IterType");
			var TAG_ITERID : QName = new QName(iNS, "IterId");
			var TAG_TITLE : QName = new QName(iNS, "Title");
			var TAG_DESCRIPTION : QName = new QName(iNS, "Description");
			var TAG_CREDITS : QName = new QName(iNS, "Credits");
			var TAG_WEBLINK : QName = new QName(iNS, "WebLink");
			var TAG_NOTE : QName = new QName(iNS, "Note");
			var TAG_STATE : QName = new QName(iNS, "State");
			var TAG_AUTHORID : QName = new QName(iNS, "AuthorId");
			var TAG_PARENTITERID : QName = new QName(iNS, "ParentIterId");
			var TAG_CREDITSNUMBER : QName = new QName(iNS, "CreditsNumber");
			
			var iters : ArrayCollection = new ArrayCollection;
			var iterType : XMLList = xres.descendants(TAG_ITERSCOLLECTION).children();
			for each(var it : XML in iterType) {
				//IterType
				var iterVO : IterVO = new IterVO;
				iterVO.id = it.child(TAG_ITERID);
				iterVO.title = it.child(TAG_TITLE);
				iterVO.description = it.child(TAG_DESCRIPTION);
				iterVO.credits = it.child(TAG_CREDITS);
				iterVO.webLink = it.child(TAG_WEBLINK);
				iterVO.notes = it.child(TAG_NOTE);
				iterVO.state = it.child(TAG_STATE);
				iterVO.authorId = it.child(TAG_AUTHORID);
				iterVO.parentIterId = it.child(TAG_PARENTITERID);
				iterVO.creditsNumber = it.child(TAG_CREDITSNUMBER);
				
				iters.addItem(iterVO);
			}
			
			StatsModelLocator.getInstance().itinera = iters;
		}
	
		public function fault( event : Object ) : void
		{
			var faultEvent : FaultEvent = FaultEvent( event );
			Alert.show( "GetPublishItersCommand.fault()!" );
		}
	}

}